package game.phases 
{
	import game.phases.actionphase.MarchSolver;
	import game.phases.actionphase.RaidSolver;
	import game.phases.actionphase.Solver;
	import game.phases.actionphase.SupportSolver;
	import game.unitmanager.ArmyController;
	/**
	 * ...
	 * @author Sulus Ltd.
	 */
	public class ActionPhase extends Phase
	{
		private var solvers:Array;
		private var currentSolverIdx:int;
		private var currentSolver:Solver;
		
		public function ActionPhase() 
		{
			
		}
		
		override public function Enter():void 
		{
			papa.gui.SetPhaseName("Action phase",OnIntroFinish);
			
		}
		
		private function OnIntroFinish():void 
		{
			solvers = [
				RaidSolver,
				SupportSolver,
				MarchSolver
			];
			currentSolverIdx = 0;
			Solve(currentSolverIdx);
		}
		
		private function Solve(idx:int):void 
		{
			currentSolverIdx = idx;
			currentSolver = new solvers[currentSolverIdx];
			currentSolver.papa = this;
			currentSolver.Enter();
		}
		
		override public function Exit():void 
		{
			//clear all orders
			//clear supports
			var allArmies:Array = papa.unitsManager.GetArmies();
			for each (var item:ArmyController in allArmies) 
			{
				item.SetOrder();
				item.supportProvince = null;
			}
			
		}
		
		public function SolverFinished():void
		{
			currentSolver.Exit();
			currentSolver = null;
			if (currentSolverIdx+1 < solvers.length)
			{
				currentSolverIdx++;
				Solve(currentSolverIdx);
			} else
			{
				Finish();
			}
		}
		
	}

}